<?php

namespace Model;

use Method\JSON;

class User
{
    public $id;
    public $username;
    public $email;
    public $nickname;
    public $avatar;
    public $registtime;
    public $lasttime;
    public $bg;
    public $personal;
    public $towho;
    private $password = 'errorpaasword';


    /**
     * get user by email
     * param $email string email address
     * return User
     */
    public static function GetUserByEmail($email)
    {
        global $pdo;
        $statement = $pdo->prepare("SELECT * FROM user WHERE email=?");
        $statement->bindValue(1, $email);
        $statement->execute();
        return $statement->fetchObject(__CLASS__);//返回一个对象
    }

    /**
     * get user by username
     * param $username string username
     * return User
     */
    public static function GetUserByUsername($username)
    {
        global $pdo;
        $statement = $pdo->prepare("SELECT * FROM user WHERE username=?");
        $statement->bindValue(1, $username);
        $statement->execute();
        return $statement->fetchObject(__CLASS__);
    }

    /**
     * get user by id
     * param $id string id
     * return User
     */
    public static function GetUserById($id)
    {
        global $pdo;
        $statement = $pdo->prepare("SELECT * FROM user WHERE id=?");
        $statement->bindValue(1, $id);
        $statement->execute();
        return $statement->fetchObject(__CLASS__);
    }

    /**
     * insert user into database
     *
     */
    public function InsertToDB()
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $this->nickname = $this->username;
        $this->avatar = 'Lib/Resource/Image/baseavatar.jpg';
        $this->bg = 'Lib/Resource/Image/basebackground.jpg';
        $statament = $pdo->prepare("INSERT INTO user SET username=:username,`password`=:pwd,email=:email,nickname=:nickname,avatar=:avatar,registtime=:rtime,bg=:bg");
        $statament->bindValue(':username', $this->username);//预定义常量string类型
        $statament->bindValue(':pwd', $this->password);
        $statament->bindValue(':email', $this->email);
        $statament->bindValue(':nickname', $this->nickname);
        $statament->bindValue(':avatar', $this->avatar);
        $statament->bindValue(':rtime', $this->registtime);
        $statament->bindValue(':bg', $this->bg);
        $statament->execute();
        $this->id = $pdo->lastInsertId();
        Follow::addfollow($this->id, $this->id);
        if (!$inTransaction) {
            $pdo->commit();
        }
        return $this->id;
    }


    public function InsertLastTimeToDB()
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $time = date("Y-m-d H:i:s");
        $statament = $pdo->prepare("UPDATE user SET lasttime=? WHERE id=?");
        $statament->bindValue(1, $time);
        $statament->bindValue(2, $this->id);
        $statament->execute();
        if (!$inTransaction) {
            $pdo->commit();
        }
    }

    /**
     * 验证帐号存在
     * */
    public static function IsUsername($username)
    {
        global $pdo;
        $statement = $pdo->prepare("SELECT COUNT(*) FROM user WHERE username=?");
        $statement->bindValue(1, $username);
        $statement->execute();
        return $statement->fetchColumn();
    }


    /**
     * 密码加密
     */
    public function verifyPassword($password)
    {
        if ($this->password == md5($password)) {
            self::InsertLastTimeToDB();
            $this->towho = $this->username;
            return true;
        } else {
            return false;
        }
    }

    /**
     * save the password
     */
    public function savePassword($password)
    {
        global $pdo;
        $pwd = md5($password);
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $statament = $pdo->prepare("UPDATE user SET password=:pwd WHERE id=:userId");
        $statament->bindValue(':pwd', $pwd);
        $statament->bindValue(':userId', $this->id);
        $statament->execute();
        if (!$inTransaction) {
            $pdo->commit();
        }
        if ($_SESSION['user']) {
            $_SESSION['user']->password = $pwd;
        }
    }


    /**
     * 测重名
     * */
    public static function verifyname($name)
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $statament = $pdo->prepare("SELECT COUNT(*) FROM user WHERE username=?");
        $statament->bindValue(1, $name);
        $statament->execute();
        $result = $statament->fetchColumn();
        if (!$inTransaction) {
            $pdo->commit();
        }
        echo $result;
    }

    public static function verifyemail($email)
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $statement = $pdo->prepare("SELECT COUNT(*) FROM user WHERE email=?");
        $statement->bindValue(1, $email);
        $statement->execute();
        $result = $statement->fetchColumn();
        if (!$inTransaction) {
            $pdo->commit();
        }
        echo $result;
    }


    /**
     * save the setting to datebase
     * */
    public function uploadzl()
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $statament = $pdo->prepare("UPDATE user SET nickname=:nickname,personal=:personal WHERE id=:userid");
        $statament->bindValue(':nickname', $this->nickname);
        $statament->bindValue(':personal', $this->personal);
        $statament->bindValue(':userid', $this->id);
        $statament->execute();
        if (!$inTransaction) {
            $pdo->commit();
        }
        $_SESSION['user']->nickname = $this->nickname;
        $_SESSION['user']->personal = $this->personal;
    }

    public function uploadzh()
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $statament = $pdo->prepare("UPDATE user SET username=:username,email=:email WHERE id=:userid");
        $statament->bindValue(':username', $this->username);
        $statament->bindValue(':email', $this->email);
        $statament->bindValue(':userid', $this->id);
        $statament->execute();
        if (!$inTransaction) {
            $pdo->commit();
        }
        $_SESSION['user']->username = $this->username;
        $_SESSION['user']->email = $this->email;
    }

    public function uploadmm()
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $statament = $pdo->prepare("UPDATE user SET nickname=:nickname,personal=:personal WHERE id=:userid");
        $statament->bindValue(':nickname', $this->nickname);
        $statament->bindValue(':personal', $this->personal);
        $statament->bindValue(':userid', $this->id);
        $statament->execute();
        if (!$inTransaction) {
            $pdo->commit();
        }
        $_SESSION['user']->nickname = $this->nickname;
        $_SESSION['user']->personal = $this->personal;
    }

    /**
     * 搜索功能
     * */
    public static function Getuserinfo($info)
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();
        if (!$inTransaction) {
            $pdo->beginTransaction();
        }
        $info = strtoupper($info);
        $info = str_split($info);
        $search = "SELECT id,username,nickname,avatar FROM user WHERE binary ucase(nickname) LIKE ";
        $search1 = "";
        $search2 = "";
        for ($i = 0; $i < count($info); $i++) {
            if ($i == 0) {
                $search1 .= "'%$info[$i]%";
                $search2 .= "'%$info[$i]%";
            } else {
                $search1 .= "$info[$i]%";
                $search2 .= "$info[$i]%";
            }
        }

        $search = $search . $search1 . "' OR binary ucase(username) LIKE " . $search2 . "' ORDER BY nickname DESC limit 4";
        $statament = $pdo->prepare($search);
        $statament->setFetchMode(\PDO::FETCH_ASSOC);
        $statament->execute();
        $result = $statament->fetchAll();
        if (!$inTransaction) {
            $pdo->commit();
        }
        JSON::json($result);
    }

    /**
     * 将username转换成id
     * */
    public static function GetIdByUsername($username)
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statament = $pdo->prepare('SELECT id FROM user WHERE username=?');
        $statament->bindValue(1, $username);
        $statament->execute();
        $id = $statament->fetch()[0];
        if (!$inTransaction) {
            $pdo->commit();
        }
        return $id;
    }

    /**
     * 将id转换成username
     * */
    public static function GetUsernameById($userid)
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statament = $pdo->prepare('SELECT username FROM user WHERE id=?');
        $statament->bindValue(1, $userid);
        $statament->execute();
        $username = $statament->fetch()[0];
        if (!$inTransaction) {
            $pdo->commit();
        }
        return $username;
    }

}